package test.design_pattern.castle_game.step2;

public class Room {

    private String description;
    private Room northExit;
    private Room southExit;
    private Room eastExit;
    private Room westExit;

    public Room(String description) {
        this.description = description;
    }

    /**
     * Room对外部的接口
     */
    public void setExits(Room north, Room east, Room south, Room west) {
        if (north != null)
            northExit = north;
        if (east != null)
            eastExit = east;
        if (south != null)
            southExit = south;
        if (west != null)
            westExit = west;
    }

    /**
     * 用接口来实现聚合
     * 1。给Room类实现的新方法，把方向的细节彻底隐藏在Room类内部了
     * 2。今后方向如何实现就和外部无关了
     * <p>
     * 可扩展性：用容器来实现灵活性
     * 1。Room的方向是用成员变量表示的，增加或减少方向就要改变代码
     * 2。如果用Hash表来保存方向，那么方向就不是"硬编码"的了。方向本身就和Room没有关系了。
     *
     * @return
     */
    public Room goRoom(String direction) {
        Room nextRoom = null;
        if (direction.equals("north")) {
            nextRoom = this.northExit;
        }
        if (direction.equals("east")) {
            nextRoom = this.eastExit;
        }
        if (direction.equals("south")) {
            nextRoom = this.southExit;
        }
        if (direction.equals("west")) {
            nextRoom = this.westExit;
        }
        return nextRoom;
    }

    /**
     * Room对外部的接口
     */
    public String getExitDesc() {
        StringBuffer s = new StringBuffer();
        if (this.northExit != null) {
            s.append("north ");
        }
        if (this.eastExit != null) {
            s.append("east ");
        }
        if (this.southExit != null) {
            s.append("south ");
        }
        if (this.westExit != null) {
            s.append("west ");
        }
        return s.toString();
    }

    @Override
    public String toString() {
        return description;
    }
}
